//
//  TouchViewController.swift
//  HHSwift
//
//  Created by 华宏 on 2020/9/8.
//  Copyright © 2020 huahong. All rights reserved.
//

import UIKit

class TouchViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "触摸手势"
        
        self.view.addGestureRecognizer(self.tap)
        self.view.addGestureRecognizer(self.pan)
        self.view.addGestureRecognizer(self.pinch)
        self.view.addGestureRecognizer(self.swipe)
        
        //轻扫发生时，平移失效
        self.pan.require(toFail: self.swipe)
        
        
    }
    
 
//MARK: - 单击、双击手势
    lazy var tap: UITapGestureRecognizer = {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(sender:)))
        tap.numberOfTouchesRequired = 1 //手指个数
        tap.numberOfTapsRequired = 1    //连续点击次数
        
        return tap
    }()

    @objc func tapAction(sender:UITapGestureRecognizer){
        
    }
    
//MARK: - 平移
    lazy var pan: UIPanGestureRecognizer = {
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction(sender:)))
        pan.minimumNumberOfTouches = 1 //最少手指数，默认1
        pan.maximumNumberOfTouches = 5 //最多手指数
        return pan
    }()
    
    @objc func panAction(sender:UIPanGestureRecognizer){
        if sender.state == .began {
            
            let point = sender.location(in: sender.view)
            print("began point:",NSCoder.string(for: point))
            
        } else if sender.state == .ended {
            
             let point = sender.location(in: sender.view)
             print("began point:",NSCoder.string(for: point))
        }
        
        let point = sender.translation(in: sender.view)
        self.view.transform.translatedBy(x: point.x, y: point.y);
//        self.view.transform = self.view.transform.translatedBy(x: point.x, y: point.y);
        
        // 恢复到初始状态
        sender.setTranslation(CGPoint.zero, in: sender.view)

    }
    
//MARK: - 捏合
    lazy var pinch: UIPinchGestureRecognizer = {
        let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
        pinch.delegate = self
        return pinch
    }()
    
    @objc func pinchAction(sender:UIPinchGestureRecognizer){
        if sender.state == .changed {
            sender.view?.transform.scaledBy(x: sender.scale, y: sender.scale)
        } else if sender.state == .ended {
            UIView.animate(withDuration: 0.25) {
                //还原到最初状态
                sender.view?.transform = CGAffineTransform.identity
            }
        }
    }
    
 //MARK: - 轻扫
    lazy var swipe: UISwipeGestureRecognizer = {
        let swipe = UISwipeGestureRecognizer(target: self, action: #selector(swipeAction(sender:)))
        return swipe
    }()
    
    @objc func swipeAction(sender:UISwipeGestureRecognizer){
       
    }
    
//MARK: - 旋转
    lazy var rotation: UIRotationGestureRecognizer = {
        let rotation = UIRotationGestureRecognizer(target: self, action: #selector(rotationAction(sender:)))
        return rotation
    }()
    
    @objc func rotationAction(sender:UIRotationGestureRecognizer){
           if sender.state == .changed {
            sender.view?.transform.rotated(by: sender.rotation)
             } else if sender.state == .ended {
                 UIView.animate(withDuration: 0.25) {
                     //还原到最初状态
                     sender.view?.transform = CGAffineTransform.identity
                 }
             }
     }
    
//MARK: - 长按
    
    lazy var longPress: UILongPressGestureRecognizer = {
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(sender:)))
        longPress.minimumPressDuration = 2.0
        longPress.allowableMovement = 10 // 误差范围 10个单位
        return longPress
    }()
    @objc func longPressAction(sender:UILongPressGestureRecognizer){
       
    }
    
//MARK: - hitTest
/*
    // 返回值：接受事件的视图
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        /**
         *  1. 循环遍历当前视图的所有子视图
         *  2. 判断当前的触摸点，是否再此子视图的范围之内
         *  3. 如果触摸点在此子视图的范围之内，那么将此视图作为返回值返回
         *  4. 返回的视图对象是最终接受时间的对象，接收事件之后，调用touchesBegan方法
         */
        
        // 1.循环遍历每一个子视图
        for (UIView *subView in self.subviews) {
            
            // 2.将触摸的坐标点，转换成子视图的坐标点
            CGPoint p = [self convertPoint:point toView:subView];
            
            // 3.判断坐标点是否再此视图的范围之内
            if ([subView pointInside:p withEvent:event]) {
                return subView;
            }
            
        }
        return [super hitTest:point withEvent:event];
    }
*/
}

//MARK: - UIGestureRecognizerDelegate
extension TouchViewController:UIGestureRecognizerDelegate{
       
    //开始进行手势识别时调用的方法，返回NO则结束识别，不再触发手势，用处：可以在控件指定的位置使用手势识别
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool{
        
        return true
    }

    
    
    // return YES允许两者同时识别。默认实现返回NO（默认情况下，不能同时识别两个手势）
    // //是否支持多手势触发，返回YES，则可以多个手势一起触发方法，返回NO则为互斥
    //是否允许多个手势识别器共同识别，一个控件的手势识别后是否阻断手势识别继续向下传播，默认返回NO；
//如果为YES，响应者链上层对象触发手势识别后，如果下层对象也添加了手势并成功识别也会继续执行，否则上层对象识别后则不再继续传播

    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool{
        
        return true
    }

    
    //这个方法返回YES，第一个手势和第二个互斥时，第一个会失效
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool{
        
        return true
    }

    // 这个方法返回YES，第一个和第二个互斥时，第二个会失效
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool{
        
        return true
    }

    
    //手指触摸屏幕后回调的方法，返回NO则不再进行手势识别，方法触发等
//    此方法在window对象在有触摸事件发生时，调用gesture recognizer的touchesBegan:withEvent:方法之前调用，
//    如果返回NO,则gesture recognizer不会看到此触摸事件。(默认情况下为YES)
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
        
        return true
    }

    
    // called before pressesBegan:withEvent: is called on the gesture recognizer for a new press. return NO to prevent the gesture recognizer from seeing this press
    @available(iOS 9.0, *)
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive press: UIPress) -> Bool{
        
        return true
    }
    
}
